﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UI.View.Chaobiao
{
    /// <summary>
    /// Elec.xaml 的交互逻辑
    /// </summary>
    public partial class Gas : Page
    {
        RestSharp.RestClient client = new RestSharp.RestClient(new Uri(Comm.TempData.BaseUrl));
        ViewModel.ChaobiaoViewModel vm;
        public Gas()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.ChaobiaoViewModel;
            vm.MeterType = "天然气";
            LoadData();
        }

        private void Btn_Chaobiao_Click(object sender, RoutedEventArgs e)
        {
            
          vm.Dialog =  HandyControl.Controls.Dialog.Show(new Part.WaterEdit());
        }


        public void LoadData()
        {
            

            var context = new Model.DataContext();
            vm.ListWater = context.DB.Queryable<Model.F_Water>().Where(w => w.MeterType == "天然气").ToList();

            var buildList = context.DB.Queryable<Model.Building>().ToList();
            cmb_Build.ItemsSource = buildList;
            cmb_Build.SelectedValuePath = "Id";
            cmb_Build.DisplayMemberPath = "Name";

        }

        private async void btn_Delete_Clicl(object sender, RoutedEventArgs e)
        {
            var res = new RestRequest("water/" + vm.CurrWater.Id, Method.DELETE);

            var api = new PmsApi();
            var ret = await api.Delete<bool>(res);
            if (ret)
            {
                HandyControl.Controls.Growl.SuccessGlobal("删除成功！");
            }
            else
            {
                HandyControl.Controls.Growl.ErrorGlobal("删除失败！");
            }

            LoadData();
        }
        private void Btn_Query_Click(object sender, RoutedEventArgs e)
        {
            vm.Query(cmb_Build.SelectedValue.ToString(), txt_roomNo.Text, txt_OwnerName.Text);
        }
    }
}
